package com.spring.component.rabbitmq.config;

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

import com.spring.component.rabbitmq.Properties;
import org.springframework.context.annotation.Configuration;

/**
 * @author liushihua
 * @since 2018年04月11日
 */
@Configuration
public class LogserviceRabbitConfig extends RabbitConfig {
  @Autowired
  private Properties rabbitProperties;

  @Bean("logserviceConnectionFactory")
  public ConnectionFactory connectionFactory() {
    if (rabbitProperties.validLogQueue()) {
      return this.instanceConnectionFactory(rabbitProperties.getLogHost(),
          Integer.parseInt(rabbitProperties.getLogPort()), rabbitProperties.getLogUsername(),
          rabbitProperties.getLogPassword(), rabbitProperties.getLogVirtualHost());
    } else {
      log.warn("没有配置日志服务mq参数，将不能使用日志服务 mq");
      return new CachingConnectionFactory();
    }
  }

  @Bean("logserviceRabbitTemplate")
  public RabbitTemplate rabbitTemplate(
      @Qualifier("logserviceConnectionFactory") ConnectionFactory connectionFactory) {
    return this.instanceRabbitTemplate(connectionFactory);
  }

  @Bean("logserviceRlcFactory")
  public SimpleRabbitListenerContainerFactory containerFactory(
      @Qualifier("logserviceConnectionFactory") ConnectionFactory connectionFactory) {
    return this.instanceFactory(connectionFactory);
  }

}
